/*

Frequency of use: 1 2 [3] 4 5 MEDIUM

Short I:
    Template method provides an implementation in a derived class, to be used by 
the base class.

Short II:
    In software engineering, the template method pattern is a design pattern. It 
is a behavioral pattern, and is unrelated to C++ templates.

Information sources:
 - http://calumgrant.net/patterns/index.html
 - http://en.wikipedia.org/wiki/Template_method_pattern
 - http://www.dofactory.com/Patterns/Patterns.aspx
*/

#include <string>
#include <iostream>

class HelloWorld
{
public:
	virtual ~HelloWorld() { }
	void output()
	{
		write_string("Hello world!");
		write_endl();
	};
	virtual void write_string(const std::string &)=0;
	virtual void write_endl()=0;
};

class HelloWorldImpl : public HelloWorld
{
public:
	void write_string(const std::string & str)
	{
		std::cout << str;
	}
	void write_endl() 
	{ 
		std::cout << std::endl; 
	}
};

void hello_world(HelloWorld & hw)
{
	hw.output();
}

int main()
{
	HelloWorldImpl hw;
	hello_world(hw);
	return 0;
}

